package rc.alfresco.wfDelegates;

import org.activiti.engine.delegate.DelegateExecution;
import org.alfresco.repo.workflow.activiti.BaseJavaDelegate;
import rc.alfresco.inflection.*;
import org.apache.log4j.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: Slejus
 * Date: 13.11.18
 * Time: 09.54
 * To change this template use File | Settings | File Templates.
 */
public class InflectUserData extends BaseJavaDelegate {

    private static final Logger logger = Logger.getLogger(InflectUserData.class);

    private String inflectPersonInfo(String str, Case wordCase, boolean inflectAllStringWords) {
        String result = "";
        //String[] parts = str.split("[,\\s\\-:\\?]");
        String[] parts = str.split("[\\s\\-:\\?]");
        int size = parts.length;
        if (inflectAllStringWords) {
            for (int i = 0; i < size; i++) {
                result = result + Inflector.getCasedWord(parts[i], wordCase);
                if (i != size - 1)
                    result = result + " ";
            }
        }
        else {
            String lastWord = parts[(size - 1)];
            lastWord = Inflector.getCasedWord(lastWord, wordCase);
            parts[(size - 1)] = lastWord;
            for (int i = 0; i < size; i++) {
                result = result + parts[i];
                if (i != size - 1) {
                    result = result + " ";
                }
            }
        }
        return result;
    }

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        String fullName = (String)execution.getVariable("initiatorFullName");
        String position = (String)execution.getVariable("initiatorPosition");
        String division = (String)execution.getVariable("initiatorDivision");

        logger.debug("JobTitle: " + position);
        logger.debug("LastName: " + fullName);
        logger.debug("Organization: " + division);

        String fullNameGen = inflectPersonInfo(fullName, Case.GENITIVE, true);
        String fullNameDat = inflectPersonInfo(fullName, Case.DATIVE, true);
        String positionGen = inflectPersonInfo(position, Case.GENITIVE, true);
        String positionDat = inflectPersonInfo(position, Case.DATIVE, true);
        String divisionGen = inflectPersonInfo(division, Case.GENITIVE, false);
        String divisionDat = inflectPersonInfo(division, Case.DATIVE, false);

        logger.debug("lastNameGen: " + fullNameGen);
        logger.debug("lastNameDat: " + fullNameDat);
        logger.debug("jobTitleGen: " + positionGen);
        logger.debug("jobTitleDat: " + positionDat);
        logger.debug("organizationGen: " + divisionGen);
        logger.debug("organizationDat: " + divisionDat);

        execution.setVariable("initiatorFullNameGen", fullNameGen);
        execution.setVariable("initiatorPositionGen", positionGen);
        execution.setVariable("initiatorDivisionGen", divisionGen);
        execution.setVariable("initiatorFullNameDat", fullNameDat);
        execution.setVariable("initiatorPositionDat", positionDat);
        execution.setVariable("initiatorDivisionDat", divisionDat);
    }
}
